package ik.xcj.hostpital.manage.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import ik.xcj.hostpital.manage.ex.ServiceException;
import ik.xcj.hostpital.manage.mapper.IScheduleMapper;
import ik.xcj.hostpital.manage.pojo.dto.ScheduleAddNewDTO;
import ik.xcj.hostpital.manage.pojo.dto.ScheduleUpdateDTO;
import ik.xcj.hostpital.manage.pojo.entity.Schedule;
import ik.xcj.hostpital.manage.pojo.vo.ScheduleListItemVO;
import ik.xcj.hostpital.manage.pojo.vo.ScheduleStandardVO;
import ik.xcj.hostpital.manage.service.IScheduleService;
import ik.xcj.hostpital.manage.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 处理医生日常安排数据的业务
 *
 * @author ik.xcj
 * @version 0.0.1
 */
@Slf4j
@Service
public class ScheduleServiceImpl implements IScheduleService {

    @Autowired
    private IScheduleMapper scheduleMapper;

    @Override
    public void addNew(ScheduleAddNewDTO scheduleAddNewDTO) {
        log.debug("开始处理【新增医生日常安排数据】的业务，参数：{}", scheduleAddNewDTO);
        /*String docname = scheduleAddNewDTO.getDocname();
        int countByName = scheduleMapper.countByName(docname);
        if (countByName > 0) {
            String message = "添加医生日常安排的数据失败，医生名称已被占用";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }*/
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleAddNewDTO, schedule);
        Date date = new Date();
        schedule.setWorkDate(date);
        /*schedule.setWorkDate((Date) simpleDateFormat);*/
        log.debug("即将插入医生日常安排的数据，参数：{}", schedule);
        int rows = scheduleMapper.insert(schedule);
        if (rows != 1) {
            String message = "插入医生日常安排的数据失败，服务器忙，请稍后再试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除医生日常安排数据】的业务，参数：{}", id);
        ScheduleStandardVO standardVO = scheduleMapper.getStandardById(id);
        if (standardVO == null) {
            String message = "删除医生日常安排数据失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将删除医生日常安排数据，参数：{}", id);
        int rows = scheduleMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除医生日常安排数据失败，服务器忙，请稍后尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void update(Long id, ScheduleUpdateDTO scheduleUpdateDTO) {
        log.debug("开始处理【修改医生日常安排数据】的业务，id={},新的数据={}", id, scheduleUpdateDTO);
        ScheduleStandardVO standardVO = scheduleMapper.getStandardById(id);
        if (standardVO == null) {
            String message = "修改医生日常安排数据失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Schedule schedule = new Schedule();
        BeanUtils.copyProperties(scheduleUpdateDTO, schedule);
        schedule.setId(id);
        Date date = new Date();
        schedule.setWorkDate(date);
        log.debug("即将修改医生日常安排数据，参数：{}", schedule);
        int rows = scheduleMapper.update(schedule);
        if (rows != 1) {
            String message = "修改医生日常安排数据失败，服务器忙，请稍后尝试！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }
    }

    @Override
    public ScheduleStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查找医生日常安排数据】，参数：{}", id);
        ScheduleStandardVO scheduleStandardVO = scheduleMapper.getStandardById(id);
        if (scheduleStandardVO == null) {
            String message = "查找医生日常安排数据失败，尝试访问的数据不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        log.debug("即将返回查询结果", scheduleStandardVO);
        return scheduleStandardVO;
    }

    @Override
    public PageInfo<ScheduleListItemVO> list(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        log.debug("开始处理【查询科室列表】的业务，参数：无");
        List<ScheduleListItemVO> scheduleList = scheduleMapper.list();
        log.debug("即将返回查询结果：{}", scheduleList);
        return new PageInfo<>(scheduleList);
    }

}
